﻿using System;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;

namespace EconomicInformationServiceWcf.ServiceBehaviours
{
    [AttributeUsage(AttributeTargets.Interface)]
    public class EISApplicationAttribute : Attribute, IContractBehavior
    {
        public void Validate(ContractDescription contractDescription, ServiceEndpoint endpoint)
        {}

        public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime)
        {
            //todo create infrastructure for registered type nedeed for specific service
            Application.Start();
            
            var inspector = new EISMessageInspector();
            dispatchRuntime.MessageInspectors.Add(inspector);

            foreach (var op in dispatchRuntime.Operations)
                op.ParameterInspectors.Add(new NullParameterInspector());

            if (dispatchRuntime.EndpointDispatcher.ContractName != "IMetadataExchange")
                dispatchRuntime.InstanceProvider = new UnityInstanceProvider(endpoint.Contract.ContractType);
        }

        public void ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {}

        public void AddBindingParameters(ContractDescription contractDescription, ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
        {}
    }
}